package com.example.fixsys.controller;

import com.example.fixsys.common.Result;
import com.example.fixsys.entity.Records;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class RecordsController {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @GetMapping("/getRecords")
    public Result<?> GetRecords(){
        String sql = "select * from record";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return Result.success(list);
    }
    @PutMapping("/addRecords")
    public Result<?> AddRecords(@RequestBody Records records){
        String sql="insert into record(recordNo,fixManager,fixWorker,fixApplicant,fixTime,fixStatus,deviceNo,applicantSay,applicantStar,workerSay,workerStar,reason,photo0,photo1,photo2) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try{
            jdbcTemplate.update(sql, new Object[]{records.getDeviceNo(),records.getFixManager(),records.getFixWorker(),records.getFixApplicant(),records.getFixTime(),records.getFixStatus(),records.getDeviceNo(),records.getApplicantSay(),records.getApplicantStar(),records.getWorkerSay(), records.getWorkerStar(), records.getReason(), records.getPhoto0(), records.getPhoto1(), records.getPhoto2()});
        }catch(Exception e){
            return Result.error("12138","项目已存在");
        }
        return Result.success();
    }

    @PostMapping("/updateRecords")
    public Result<?> UpdateRecords(@RequestBody Records records){
        String sql="update record set fixManager=?,fixWorker=?,fixApplicant=?,fixTime=?,fixStatus=?,deviceNo=?,applicantSay=?,applicantStar=?,workerSay=?,workerStar=?,reason=?,photo0=?,photo1=?,photo2=? where recordNo=?";
        try{
            jdbcTemplate.update(sql, new Object[]{records.getFixManager(),records.getFixWorker(),records.getFixApplicant(),records.getFixTime(),records.getFixStatus(),records.getDeviceNo(),records.getApplicantSay(),records.getApplicantStar(),records.getWorkerSay(), records.getWorkerStar(),records.getReason(), records.getPhoto0(), records.getPhoto1(), records.getPhoto2(),records.getDeviceNo()});
        }catch (Exception e){
            return Result.error("12138","项目不存在");
        }
        return Result.success();
    }

    @DeleteMapping("/deleteRecords")
    public Result<?> DeleteRecords(@RequestBody Records records){
        String sql="delete from record where recordNo=?";
        try{
            jdbcTemplate.update(sql, new Object[]{records.getRecordNo()});
        }catch (Exception e){
            return Result.error("12138","项目不存在");
        }
        return Result.success();
    }

    @GetMapping("/checkTheRecord")
    public Result<?> CheckTheRecord(@RequestParam Integer recordNo){
        String sql = "select * from record where recordNo=? ";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{recordNo});
        return Result.success(list);
    }

    @GetMapping("/checkApplicantRecords")
    public Result<?> CheckApplicantRecords(@RequestParam Integer fixApplicant){
        String sql = "select * from record where fixApplicant=? ";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql,new Object[]{fixApplicant});
        return Result.success(list);
    }

    @GetMapping("/checkUnfinishedRecords")
    public Result<?> CheckUnfinishedRecords(){
        String sql = "select * from record where fixStatus=0 ";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return Result.success(list);
    }

    @PostMapping("/updateRecordStatus")
    public Result<?> UpdateDevice(@RequestBody Records records){
        String sql="update record set fixStatus=1 where recordNo=?";
        try {
            jdbcTemplate.update(sql, new Object[]{records.getRecordNo()});
        }catch(Exception e){
            return Result.error("12138","项目已存在");
        }
        return Result.success();
    }

    @PostMapping("/seizeOrders")
    public Result<?> SeizeOrders(@RequestBody Records records){
        String sql="update record set fixWorker=? where recordNo=?";
        try {
            jdbcTemplate.update(sql, new Object[]{records.getFixWorker(),records.getRecordNo()});
        }catch(Exception e){
            return Result.error("12138","项目已存在");
        }
        return Result.success();
    }

    @PostMapping("/applicantAppraise")
    public Result<?> ApplicantAppraise(@RequestBody Records records){
        String sql="update record set applicantStar=?,applicantSay=? where recordNo=?";
        try {
            jdbcTemplate.update(sql, new Object[]{records.getApplicantStar(),records.getApplicantSay(),records.getRecordNo()});
        }catch(Exception e){
            return Result.error("12138","项目已存在");
        }
        return Result.success();
    }

    @PostMapping("/workerAppraise")
    public Result<?> WorkerAppraise(@RequestBody Records records){
        String sql="update record set workerStar=?,workerSay=? where recordNo=?";
        try {
            jdbcTemplate.update(sql, new Object[]{records.getWorkerStar(),records.getWorkerSay(),records.getRecordNo()});
        }catch(Exception e){
            return Result.error("12138","项目已存在");
        }
        return Result.success();
    }
}
